import { usermenu, changeuserstate } from '../api/index.js'
import { Message } from 'element-ui'
export default {
  state: {
    queryInfo: {
      // 搜索值
      query: '',
      // 当前的页数
      pagenum: 1,
      // 当前每次显示多少条数据
      pagesize: 2
    },
    dispathdialog: false,
    rightsuser: {},
    setroles: [],
    userList: {},
    total: 0,
    tableData2: [
      {
        id: 510,
        state: true,
        date: '2016-05-02',
        name: '王1',
        address: '上海市普陀区金沙江路 1518 弄'
      },
      {
        id: 500,
        state: true,
        date: '2016-05-04',
        name: '王2',
        address: '上海市普陀区金沙江路 1519 弄'
      },
      {
        id: 503,
        state: false,
        date: '2016-05-01',
        name: '王3',
        address: '上海市普陀区金沙江路 1520 弄'
      },
      {
        id: 505,
        state: true,
        date: '2016-05-03',
        name: '王4',
        address: '上海市普陀区金沙江路 1521 弄'
      }
    ],
    dialogVisible: false,
    addForm: {
      username: '',
      password: '',
      email: '',
      mobile: ''
    },
    editdialog: false,
    edituserForm: {}
  },
  mutations: {
    adduserList(state, val) {
      state.userList = val.data.users
      state.total = val.data.totalpage
    },
    changepagesize(state, val) {
      state.queryInfo.pagesize = val
    },
    async getusermenu(state) {
      const { data: res } = await usermenu(state.queryInfo)
      // console.log(res)
      if (res.meta.status !== 200) {
        return Message.error(res.meta.msg)
      } else {
        state.adduserList(res)
      }
    },
    changepagenum(state, val) {
      state.queryInfo.pagenum = val
    },
    async changesState(state, val) {
      const { data: res } = await changeuserstate(val)
      if (res.meta.status !== 200) {
        val.state = !val.state
        return Message.error(res.meta.msg)
      } else {
        return Message.success('更新成功')
      }
    },
    changedialogVisible(state, val) {
      state.dialogVisible = val
    },
    changeeditdialog(state, val) {
      state.editdialog = val
    },
    setedituserForm(state, val) {
      state.edituserForm = val
    },
    changedispathdialog(state, val) {
      state.dispathdialog = val
    },
    getrightsuser(state, val) {
      state.rightsuser = val
    },
    setsetroles(state, val) {
      state.setroles = val
    }
  }
}
